﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SMS.ServiceManager;
using digit.Core;
using SMS.UserManager;

namespace SMS.Admin.Modules
{
    public partial class DetailSportCup : WebControlBase
    {
        private int cupId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!ClientState.IsAuthenticated)
            {
                Response.Redirect("~/Login.aspx?session=timeout");
            }

            cupId = Request.QueryString["cupId"] != null ? Int32.Parse(Request.QueryString["cupId"].ToString().Trim()) : -1;

            if (ClientState.MemberInfo.UserType != 0 && ClientState.MemberInfo.UserType != 1)
            {
                if (cupId == -1)
                {
                    lnkAdd.Enabled = false;
                    lnkAdd.Enabled = false;
                }
            }

            if (IsPostBack) return;

            BindInfo();
        }

        protected void lnkAdd_Click(object sender, System.EventArgs e)
        {
            if (Page.IsValid)
            {
                DoSave();
            }
        }

        protected void lnkBack_Click(object sender, System.EventArgs e)
        {
            Response.Redirect("Default.aspx?module=ListSportCup");
        }

        private void BindInfo()
        {
            SportCupInfo objCup = SportCupManager.Select(cupId);

            if (objCup != null)
            {
                this.txtCupName.Text = objCup.CupName;
                this.ddlType.SelectedValue = objCup.CupType.ToString();
                this.txtCupCode.Text = objCup.CupCode;
                this.txtCupCodeOpen.Text = objCup.CupCodeOpen;
            }
        }

        private bool DoSave()
        {
            try
            {
                bool result = false;
                if (cupId > 0)
                {
                    SportCupInfo objCup = SportCupManager.Select(cupId);
                    if (objCup != null)
                    {
                        objCup.CupID = cupId;
                        objCup.CupName = this.txtCupName.Text;
                        objCup.CupType = byte.Parse(this.ddlType.SelectedValue);
                        objCup.CupCode = this.txtCupCode.Text;
                        objCup.CupCodeOpen = this.txtCupCodeOpen.Text;

                        result = SportCupManager.Update(objCup);
                        if (result)
                            lblError.Text = "Cập nhật thành công!";
                        else
                            lblError.Text = "Cập nhật lỗi";
                    }
                }
                else
                {
                    SportCupInfo objCup = new SportCupInfo();
                    objCup.CupName = this.txtCupName.Text;                    
                    objCup.CupCode = this.txtCupCode.Text;
                    objCup.CupCodeOpen = this.txtCupCodeOpen.Text;
                    objCup.CupType = byte.Parse(this.ddlType.SelectedValue);

                    result = SportCupManager.Insert(objCup);
                    if (result)
                        lblError.Text = "Cập nhật thành công!";
                    else
                        lblError.Text = "Cập nhật lỗi";
                }
                return result;
            }
            catch
            {
                return false;
            }
        }
    }
}
